package com.atguigu.tingshu.album.config;

import jakarta.annotation.PostConstruct;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/***
 * 开启可靠性传递
 */
@Component
@Log4j2
public class AlbumAndSearchReturnCallBackConfig implements RabbitTemplate.ReturnsCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnsCallback(this);
    }

    /**
     * 消息没有抵达队列时触发，记录到日志
     * @param returnedMessage
     */
    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        //记录没有抵达队列的消息id、交换机和roundkey
        log.error("进入队 列失败的消息ID是："+ new String(returnedMessage.getMessage().getBody())+
                "失败的交换机是："+returnedMessage.getExchange() +
                "roundKey是："+returnedMessage.getRoutingKey());
    }
}
